/*
Disclaimer of Warranty: THE PACKAGE
IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT
ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT
ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW.
UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR
WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(c) Copyright 2013 Edward Principe
All Rights Reserved
*/
#include "stdafx.h"
#include "errrep.h"

////////////////////////////////////////////////////////////////
// Copy: Copies the class
/////////////////////////////////////////////////////////////////
void ErrRep::Copy(const ErrRep& rhs)
{
    if(this == &rhs)
        return;

    _errNodes = rhs._errNodes;
    _pos = rhs._pos;
    _errors = rhs._errors;
    _warnings = rhs._warnings;
}

////////////////////////////////////////////////////////////////////
// Append: Appends errors to the class
///////////////////////////////////////////////////////////////
void ErrRep::Append(const ErrRep& rhs)
{
    // Traverse the list

    for(list<ErrNode>::const_iterator i = rhs._errNodes.begin(); i != rhs._errNodes.end(); i++)
    {
        _errNodes.push_back(*i);

    }

    // Append warning and error count
    _warnings += rhs._warnings;
    _errors += rhs._errors;

}

void ErrRep::Append(const ErrNode& rhs)
{
    // Append error
    _errNodes.push_back(rhs);

    // See what kind of error it is
    // And increment counter
    SmtPtr<SyntaxEx> s;

    s->PutErr(rhs.err);

    switch(s->GetSev())
    {
        case(SEV_ERROR):
            _errors++;
            break;

        case(SEV_WARNING):
            _warnings++;
            break;
    }
}

///////////////////////////////////////////////////////////////
// Set: Adds an error_
///////////////////////////////////////////////////////////////
void ErrRep::Set(SyntaxErr err, unsigned long line, unsigned long col)
{
    // Locals
    SmtPtr<ErrNode> e;

    // Put err into a buffer
    e->err = err;
    e->line = line;
    e->col = col;

    // Add error to the list
    _errNodes.push_back(*e);

    // Increment error count
    SmtPtr<SyntaxEx> s;

    s->PutErr(err);

    switch(s->GetSev())
    {
    case(SEV_WARNING):
        _warnings++;
        break;

    case(SEV_ERROR):
        _errors++;
        break;
    }
}

void ErrRep::Set(const ErrNode& errNode)
{
    _errNodes.push_back(errNode);

    SmtPtr<SyntaxEx> se;

    se->PutErr(errNode.err);

    switch(se->GetSev())
    {
    case(SEV_WARNING):
        _warnings++;
        break;

    case(SEV_ERROR):
        _errors++;
        break;
    }
}

////////////////////////////////////////////////////////////////
// GetFirst: Get first error
///////////////////////////////////////////////////////////////////
list<ErrNode>::iterator ErrRep::GetFirst()
{
    _pos = _errNodes.begin();

    return _pos;
}

///////////////////////////////////////////////////////////////////
// GetNext: Get next error
///////////////////////////////////////////////////////////////////
list<ErrNode>::iterator ErrRep::GetNext()
{
    if(_pos == _errNodes.end())
        return _pos;

    _pos++;

    return _pos;
}

///////////////////////////////////////////////////////////////////
// PrintReport: Outputs an error report to stderr
///////////////////////////////////////////////////////////////////
void ErrRep::PrintReport()
{
    SmtPtr<SyntaxEx> e;

    for(list<ErrNode>::iterator i = _errNodes.begin(); i != _errNodes.end(); i++)
    {
        e->PutErr(i->err);
#ifdef _UNICODE
        wcerr << TEXT("(") << i->line << TEXT(",") << i->col << TEXT(") ") << e->MakeMsg() << endl;
#else
        cerr << TEXT("(") << i->line << TEXT(",") << i->col << TEXT(") ") << e->MakeMsg() << endl;
#endif // _UNICODE
    }

#ifdef _UNICODE
    wcerr << TEXT("Error(s): ") << _errors << TEXT(", Warning(s): ") << _warnings << endl;
#else
    cerr << TEXT("Error(s): ") << _errors << TEXT(", Warning(s): ") << _warnings << endl;
#endif // _UNICODE
}
